home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / all macintosh / streaming / qtmoviefromurl / qtmoviefromurl.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  3.6 KB  |  137 lines

  1. //////////
  2. //
  3. //    File:        QTMovieFromURL.c
  4. //
  5. //    Contains:    Sample code for opening a QuickTime movie specified by a URL.
  6. //
  7. //    Written by:    Tim Monroe
  8. //
  9. //    Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //    Change History (most recent first):
  12. //
  13. //       <1>         10/29/98    rtm        first file
  14. //     
  15. //    QuickTime Streaming has a URL data handler, which you can use to open movies that are
  16. //     specified using uniform resource locators (URLs). A URL is the address of some resource
  17. //    on the Internet or on a local disk. The QuickTime URL data handler can open http URLs,
  18. //    ftp URLs, file URLs, and rtsp URLs.
  19. //
  20. //    This snippet defines several functions. The function QTURL_NewMovieFromURL takes a URL
  21. //    as a parameter and opens the movie file located at the specified location. You can use
  22. //    the function QTURL_GetURLBasename to get the basename of the URL (which is suitable for
  23. //    use as the title of the window you display the movie in).
  24. //
  25. //////////
  26.  
  27. #include "QTMovieFromURL.h"
  28.  
  29.  
  30. //////////
  31. //
  32. // QTURL_NewMovieFromURL
  33. // Open the movie file referenced by the specified uniform resource locator (URL).
  34. //
  35. //////////
  36.  
  37. Movie QTURL_NewMovieFromURL (char *theURL)
  38. {
  39.     Movie        myMovie = NULL;
  40.     Handle        myHandle = NULL;
  41.     Size        mySize = 0;
  42.     
  43.     //////////
  44.     //
  45.     // copy the specified URL into a handle
  46.     //
  47.     //////////
  48.     
  49.     // get the size of the URL, plus the terminating null byte
  50.     mySize = (Size)strlen(theURL) + 1;
  51.     if (mySize == 0)
  52.         goto bail;
  53.     
  54.     // allocate a new handle
  55.     myHandle = NewHandleClear(mySize);
  56.     if (myHandle == NULL)
  57.         goto bail;
  58.  
  59.     // copy the URL into the handle
  60.     BlockMove(theURL, *myHandle, mySize);
  61.  
  62.     //////////
  63.     //
  64.     // instantiate a movie from the specified URL
  65.     //
  66.     // the data reference that is passed to NewMovieFromDataRef is a handle
  67.     // containing the text of the URL, *with* a terminating null byte; this
  68.     // is an exception to the usual practice with data references (where you
  69.     // need to pass a handle to a handle containing the relevant data)
  70.     //
  71.     //////////
  72.     
  73.     NewMovieFromDataRef(&myMovie, newMovieActive, NULL, myHandle, URLDataHandlerSubType);
  74.  
  75. bail:
  76.     if (myHandle != NULL)
  77.         DisposeHandle(myHandle);
  78.         
  79.     return(myMovie);
  80. }
  81.  
  82.  
  83. //////////
  84. //
  85. // QTURL_GetURLBasename
  86. // Return the basename of the specified URL.
  87. //
  88. // The basename of a URL is the portion of the URL following the rightmost URL separator. This function
  89. // is useful for setting window titles of movies opened using the URL data handler to the basename of a
  90. // URL (just like MoviePlayer does).
  91. //
  92. // The caller is responsible for disposing of the pointer returned by this function (by calling free).
  93. //
  94. //////////
  95.  
  96. char *QTURL_GetURLBasename (char *theURL)
  97. {
  98.     char    *myBasename = NULL;
  99.     short    myLength = 0;
  100.     short    myIndex;
  101.  
  102.     // make sure we got a URL passed in
  103.     if (theURL == NULL)
  104.         goto bail;
  105.         
  106.     // get the length of the URL
  107.     myLength = strlen(theURL);
  108.     
  109.     // find the position of the rightmost URL separator in theURL
  110.     if (strchr(theURL, kURLSeparator) != NULL) {
  111.  
  112.         myIndex = myLength - 1;
  113.         while (theURL[myIndex] != kURLSeparator)
  114.             myIndex--;
  115.             
  116.         // calculate the length of the basename
  117.         myLength = myLength - myIndex - 1;
  118.  
  119.     } else {
  120.         // there is no rightmost URL separator in theURL;
  121.         // set myIndex so that myIndex + 1 == 0, for the call to BlockMove below
  122.         myIndex = -1;
  123.     }
  124.     
  125.     // allocate space to hold the string that we return to the caller
  126.     myBasename = malloc(myLength + 1);
  127.     if (myBasename == NULL)
  128.         goto bail;
  129.         
  130.     // copy into myBasename the substring of theURL from myIndex + 1 to the end
  131.     BlockMove(&theURL[myIndex + 1], myBasename, myLength);
  132.     myBasename[myLength] = '\0';
  133.     
  134. bail:    
  135.     return(myBasename);
  136. }
  137.